package cc.rengu.igas.bomp.core.realize.impl;

import cc.rengu.igas.bomp.common.constant.BompTreeNodeConstant;
import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.MchntSignInfo;
import cc.rengu.igas.bomp.common.enums.MchntStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.realize.NormalMchntInfoService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author: zhangxuran
 * @Date: 2020/4/21 19:32
 * @Description: 根据机构号，商户号查询商户信息（基础信息，签约信息，账户信息，映像信息，产品信息）.
 */
public class NormalMchntInfoServiceImpl implements NormalMchntInfoService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public MchntBaseInfo checkMchntStatus(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(instId, mchntNo);
        if (null == mchntBaseInfo) {
            rglog.error("获取商户信息失败,sysSeqNum:{},instId :{}，mchntNo:{}", sysSeqNum, instId, mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }

        if (!MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
            rglog.error("商户状态不合法,status:{},sysSeqNum:{},instId :{}，mchntNo:{}", mchntBaseInfo.getMchntStatus(), sysSeqNum, instId, mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespDesc());
        }
        xmlTreeUtil.setXmlTreeObjectValue(BompTreeNodeConstant.MCHNT_BASE_INFO, mchntBaseInfo);
        return mchntBaseInfo;
    }


    @Override
    public MchntBaseInfo getMchntBaseInfo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(instId, mchntNo);
        if (null == mchntBaseInfo) {
            rglog.error("获取商户信息失败,sysSeqNum:{},instId :{}，mchntNo:{}", sysSeqNum, instId, mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }

        xmlTreeUtil.setXmlTreeObjectValue(BompTreeNodeConstant.MCHNT_BASE_INFO, mchntBaseInfo);
        return mchntBaseInfo;
    }

    @Override
    public MchntSignInfo selectMchntSignInfo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        MchntBaseInfo mchntBaseInfo = (MchntBaseInfo) xmlTreeUtil.getXmlTreeObjectValue(BompTreeNodeConstant.MCHNT_BASE_INFO, MchntBaseInfo.class);

        return null;
    }


}
